﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using NovelLibEx.Parser;

namespace NovelLibEx
{
    public class CommonDef
    {
        public static string BaseDir { get { return "D:\\Novel"; } set { ;} }

        /// <summary>
        /// 保存网页
        /// </summary>
        /// <param name="srcUrl"></param>
        /// <param name="dstPath"></param>
        /// <returns></returns>
        public static bool DownLoadLink(string srcUrl, string dstPath)
        {
            string strContent = "";

            WebRequest request = WebRequest.Create(srcUrl);
            request.Credentials = CredentialCache.DefaultCredentials;

            try
            {
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();

                string strStatus = response.StatusDescription;
                HttpStatusCode hStatusCode = response.StatusCode;
                using (Stream dataStream = response.GetResponseStream())
                {
                    using (StreamReader reader = new StreamReader(dataStream, 
                        Encoding.GetEncoding("GB2312")))
                    {
                        strContent = reader.ReadToEnd();

                        strContent = GetParser(srcUrl).RemoveAd(strContent);
                    }
                }
                response.Close();

                StreamWriter writer = new StreamWriter(dstPath, true, Encoding.GetEncoding("GB2312"));
                writer.Write(strContent);
                writer.Close();
            }
            catch (Exception)
            {
                return false;
            }

            return true;
        }

        public static string GetSiteName(string strUrl)
        {
            if (strUrl.ToLower().Contains("www.pashu.org"))
            {
                return "www.pashu.org";
            }
            else if (strUrl.ToLower().Contains("www.sodu.org"))
            {
                return "www.sodu.org";
            }
            else if (strUrl.ToLower().Contains("www.aiduz.cn"))
            {
                return "www.aiduz.cn";
            }

            throw new KeyNotFoundException("没有找到对应的小说站点" + strUrl);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="strUrl"></param>
        /// <returns></returns>
        public static INovelParser GetParser(string strUrl)
        {
            if (strUrl.ToLower().Contains("www.pashu.org"))
            {
                return new PashuParser();
            }
            else if (strUrl.ToLower().Contains("www.sodu.org"))
            {
                return new SoduParser();
            }
            else if (strUrl.ToLower().Contains("www.aiduz.cn"))
            {
                return new AiduzParser();
            }

            throw new KeyNotFoundException("没有找到对应的小说站点" + strUrl);
        }
    }

    /// <summary>
    /// 小说信息
    /// 就是小说里面目录页面下载的信息
    /// </summary>
    /// <remarks>其实这个也没有多少的意义，可以考虑把这个去掉</remarks>
    public class NovelInfo
    {
        public string Status { get; set; }
        public string Content { get; set; }
        public HttpStatusCode StatusCode { get; set; }

        public NovelInfo(string strContent, string strStatus, HttpStatusCode statusCode)
        {
            Content = strContent;
            Status = strStatus;
            StatusCode = statusCode;
        }
    }
}
